﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using StandXunYouServer.IServices;
using StandXunYouServer.Model;
using StandXunYouServer.Utility;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace StandXunYouServer.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AnswerQuestionsController : ControllerBase
    {
        //答题API


        private readonly ILogger<AnswerQuestionsController> _logger;
        private readonly IAnswerQuestionsServices _baseConn;

        public AnswerQuestionsController(ILogger<AnswerQuestionsController> logger, IAnswerQuestionsServices baseConn)
        {
            _logger = logger;
            _baseConn = baseConn;
        }

        /// <summary>
        /// 获取随机两道题目
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetTopicQueryAsync")]
        public async Task<IActionResult> GetTopicQueryAsync()
        {
            try
            {
                var tList = await _baseConn.GetRandomTopicQueryAsync();

                return Ok(BaseState.CreateInstance(200, "请求成功", tList, tList.Count()));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, "获取题目失败");
            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求服务器失败，请稍后重试"));
        }

        /// <summary>
        /// 查询用户是否答过题
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        [HttpGet("UserIsAnswerAsync")]
        public async Task<IActionResult> UserIsAnswerAsync(string UserId) 
        {
            try
            {
                var result = await _baseConn.UserIsAnswerAsync(UserId);

                return Ok(BaseState.CreateInstance(200, "请求成功", result));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, "查询用户是否答题失败");
            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求服务器失败，请稍后重试"));
        }

        /// <summary>
        /// 查询答对的题目数量并为用户添加积分、写入用户体验表
        /// </summary>
        /// <param name="topicId">题目ID</param>
        /// <param name="options">题目选项</param>
        /// <returns></returns>
        [HttpPost("CorrectAnswerAsync")]
        public async Task<IActionResult> CorrectAnswerAsync([FromForm]string userId, [FromForm] string[] topicId, [FromForm] string[] options)
        {
            try
            {
                var correctQuestion = await _baseConn.CorrectAnswerAsync(topicId, options);
                var points = correctQuestion == 1 ? 50 : 100; //如果答对了一道题则加50积分

                ReCord model = new ReCord()
                {
                    ReCordId = Guid.NewGuid().ToString(),
                    UserId = userId,
                    UserPoints = points,
                    AnswerLevel = correctQuestion.ToString()
                };
                // 为用户添加积分并写入用户体验表
                var result = await _baseConn.UpdateUserPointsAndInsertReCordTranAsync(model.UserId, model.UserPoints, model);

                return Ok(BaseState.CreateInstance(200, "请求成功！", model.UserPoints, Convert.ToInt32(model.AnswerLevel)));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, "查询答对的题目数量并为用户添加积分、写入用户体验表失败");
            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求服务器失败，请稍后重试"));
        }

    }
}
